/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.model.entity;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 *
 * @author ronildo
 */
@Entity
@Table(name = "login")
@NamedQueries({
    @NamedQuery(name = "Login.findAll", query = "SELECT l FROM Login l"),
    @NamedQuery(name = "Login.findByPkLogin", query = "SELECT l FROM Login l WHERE l.pkLogin = :pkLogin"),
    @NamedQuery(name = "Login.findByName", query = "SELECT l FROM Login l WHERE l.name = :name"),
    @NamedQuery(name = "Login.findByPassword", query = "SELECT l FROM Login l WHERE l.password = :password"),
    @NamedQuery(name = "Login.findByEmail", query = "SELECT l FROM Login l WHERE l.email = :email"),
    @NamedQuery(name = "Login.findByDescription", query = "SELECT l FROM Login l WHERE l.description = :description"),
    @NamedQuery(name = "Login.findByBlocked", query = "SELECT l FROM Login l WHERE l.blocked = :blocked"),
    @NamedQuery(name = "Login.findByActivationKey", query = "SELECT l FROM Login l WHERE l.activationKey = :activationKey")})
public class Login implements Serializable {
    private static final long serialVersionUID = 1L;

    @Transient
    private LoginRulesView loginRulesView;

    @Id
    @Basic(optional = false)
    @Column(name = "pk_login")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="seq_login")
    @SequenceGenerator(name="seq_login", sequenceName="seq_login", allocationSize=1)
    private Long pkLogin;

    @Basic(optional = false)
    @Column(name = "name")
    private String name;
    
    @Basic(optional = false)
    @Column(name = "password")
    private String password;

    @Basic(optional = false)
    @Column(name = "email")
    private String email;

    @Column(name = "description")
    private String description;

    @Basic(optional = false)
    @Column(name = "blocked")
    private boolean blocked;

    @Basic(optional = false)
    @Column(name = "activation_key")
    private String activationKey;

    @OneToMany(mappedBy = "login")
    private List<Modem> modemList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "login")
    private List<ClientPhone> clientPhoneList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "login")
    private List<SmsSend> smsSendList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "login")
    private List<SmsReceived> smsReceivedList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "login")
    private List<KeyResponse> keyResponseList;

    @JoinColumn(name = "fk_role", referencedColumnName = "pk_role")
    @ManyToOne(optional = false)
    private Role role;

    @JoinColumn(name = "fk_company", referencedColumnName = "pk_company")
    @ManyToOne(optional = false)
    private Company company;

    @JoinColumn(name = "fk_bank_account", referencedColumnName = "pk_bank_account")
    @ManyToOne(optional = false)
    private BankAccount bankAccount;

    public void createRulesView(){
        loginRulesView = new LoginRulesView();
        if(role.getName().equals(LoginRulesView.ROOT)){
            loginRulesView.setRoot(true);
            loginRulesView.setAdministrator(true);
            loginRulesView.setClient(true);
        }else if(role.getName().equals(LoginRulesView.ADMINISTRATOR)){
            loginRulesView.setAdministrator(true);
            loginRulesView.setClient(true);
        }else if(role.getName().equals(LoginRulesView.CLIENT))
            loginRulesView.setClient(true);
    }

    public Login() {
    }

    public Login(Long pkLogin) {
        this.pkLogin = pkLogin;
    }

    public Login(Company company) {
        this.company = company;
    }

    public Login(Long pkLogin, String name, String password, String email, boolean blocked, String activationKey) {
        this.pkLogin = pkLogin;
        this.name = name;
        this.password = password;
        this.email = email;
        this.blocked = blocked;
        this.activationKey = activationKey;
    }

    public Long getPkLogin() {
        return pkLogin;
    }

    public void setPkLogin(Long pkLogin) {
        this.pkLogin = pkLogin;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean getBlocked() {
        return blocked;
    }

    public void setBlocked(boolean blocked) {
        this.blocked = blocked;
    }

    public String getActivationKey() {
        return activationKey;
    }

    public void setActivationKey(String activationKey) {
        this.activationKey = activationKey;
    }

    public List<Modem> getModemList() {
        return modemList;
    }

    public void setModemList(List<Modem> modemList) {
        this.modemList = modemList;
    }

    public List<ClientPhone> getClientPhoneList() {
        return clientPhoneList;
    }

    public void setClientPhoneList(List<ClientPhone> clientPhoneList) {
        this.clientPhoneList = clientPhoneList;
    }

    public List<SmsSend> getSmsSendList() {
        return smsSendList;
    }

    public void setSmsSendList(List<SmsSend> smsSendList) {
        this.smsSendList = smsSendList;
    }

    public List<SmsReceived> getSmsReceivedList() {
        return smsReceivedList;
    }

    public void setSmsReceivedList(List<SmsReceived> smsReceivedList) {
        this.smsReceivedList = smsReceivedList;
    }

    public List<KeyResponse> getKeyResponseList() {
        return keyResponseList;
    }

    public void setKeyResponseList(List<KeyResponse> keyResponseList) {
        this.keyResponseList = keyResponseList;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public BankAccount getBankAccount() {
        return bankAccount;
    }

    public void setBankAccount(BankAccount bankAccount) {
        this.bankAccount = bankAccount;
    }

    public LoginRulesView getLoginRulesView() {
        return loginRulesView;
    }

    public void setLoginRulesView(LoginRulesView loginRulesView) {
        this.loginRulesView = loginRulesView;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (pkLogin != null ? pkLogin.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Login)) {
            return false;
        }
        Login other = (Login) object;
        if ((this.pkLogin == null && other.pkLogin != null) || (this.pkLogin != null && !this.pkLogin.equals(other.pkLogin))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "br.com.jr2tecnologia.model.entity.Login[pkLogin=" + pkLogin + "]";
    }

}
